package org.wattdepot.cli.command;

import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * 
 * PowerStamp class queries the server for the power generated, in Watts, or consumed by a source
 * given a timestamp. This class extends CommandLineInterface to have access to the WattDepotClient
 * and implements WattDepotCliCommand interface to ensure this class does a specific command.
 * 
 * @author Edward Meyer, Bao Huy Ung
 * 
 */
public class PowerStamp extends CommandLineInterface implements WattDepotCliCommand {

  /**
   * Constructor to create an object PowerStamp to return the power generated or consumed given a
   * timestamp.
   */
  public PowerStamp() {
    // Empty constructor.
  }

  /**
   * 
   * Queries the server for the power generated or consumed in Watts, by a source given a timestamp.
   * 
   * @param args Is a list of all the arguments entered by the user.
   * @return Power generated (or consumed) by the source at the given timestamp, or an error
   * message.
   */
  public String doCommand(List<String> args) {
    // If there are too few or too many arguments, return an error message.
    if (args.size() != 5) {
      return "Error: Incorrect number of arguments were entered. Expected 5, instead "
      + args.size() + " were entered.";
    }

    Source inputSource = null;
    String inputSourceName = args.get(2);
    String inputStamp = args.get(4);
    XMLGregorianCalendar timeStamp = null;

    // Check if a valid source was entered.
    inputSource = isValidSource(inputSourceName);
    if (inputSource == null) {
      return "\"" + inputSourceName + "\" is not a valid source.";
    }

    // Attempt to parse the entered timestamp to see if it's a valid timestamp.
    timeStamp = isValidTimestamp(inputStamp);
    if (timeStamp == null) {
      return "Timestamp: \"" + inputStamp + "\" cannot be parsed into a timestamp.";
    }

    // Check if the correct keyword "timestamp" was used.
    if (!args.get(3).equalsIgnoreCase("timestamp")) {
      return "Error: Keyword \"timestamp\" was mistyped as \"" + args.get(3) + "\".";
    }

    Double outputNum = null;

    // Attempt to access and print data for consumed
    // or generated power given the entered time.
    try {
      if (args.get(1).equalsIgnoreCase("generated")) {
        outputNum = client.getPowerGenerated(inputSourceName, timeStamp);
      }
      else if (args.get(1).equalsIgnoreCase("consumed")) {
        outputNum = client.getPowerConsumed(inputSourceName, timeStamp);
      }
      else {
        return "Invalid parameter at [generated|consumed] argument.";
      }
    }
    // Catch block if the timestamp yields no data for the given timestamp.
    catch (ResourceNotFoundException r) {
      return "Source: \"" + inputSourceName + "\" contains no sensor data " + "on: \"" + inputStamp
          + "\"";
    }
    catch (MiscClientException mce) {
      return "Network communication failure when retrieving sensor data.";
    }
    // If any unknown error occurred.
    catch (Exception e) {
      return "An error occured when retrieving sensor data.\n" + e;
    }

    return outputNum.toString();
  }

}
